﻿@inject IStringLocalizer<TablesDetailRowIsDetails> Localizer
@inject IStringLocalizer<Foo> FooLocalizer

<Table TItem="Foo" @ref="@Table"
        IsPagination="true" PageItemsSource="@PageItemsSource"
        IsStriped="true" IsBordered="true" IsDetails="IsDetails"
        ShowToolbar="true" ShowDefaultButtons="false" ShowRefresh="false" OnDoubleClickRowCallback="OnDoubleClickRowCallback()!"
        OnQueryAsync="@OnQueryAsync">
    <TableColumns>
        <TableColumn @bind-Field="@context.DateTime" Width="180" />
        <TableColumn @bind-Field="@context.Name" Width="100" />
        <TableColumn @bind-Field="@context.Address" />
        <TableColumn @bind-Field="@context.Count" />
    </TableColumns>
    <DetailRowTemplate>
        <div>@Localizer["EducationText"] @typeof(EnumEducation).ToDescriptionString(context.Education.ToString())</div>
    </DetailRowTemplate>
</Table>

<Button Text="@DetailText" OnClick="OnClickDetailRow" class="mt-3"></Button>

@code {
    private static IEnumerable<int> PageItemsSource => new int[] { 4, 10, 20 };

    [NotNull]
    private List<Foo>? Items { get; set; }

    [NotNull]
    private string? DetailText { get; set; }

    [NotNull]
    private Table<Foo>? Table { get; set; }

    /// <summary>
    /// OnInitialized 方法
    /// </summary>
    protected override void OnInitialized()
    {
        base.OnInitialized();

        Items = Foo.GenerateFoo(FooLocalizer);
        DetailText = Localizer[$"{nameof(DetailText)}{IsDetails}"];
    }

    private Func<Foo, Task>? OnDoubleClickRowCallback()
    {
        Func<Foo, Task>? ret = null;
        if (IsDetails)
        {
            ret = foo =>
            {
                Table.ExpandDetailRow(foo);
                return Task.CompletedTask;
            };
        }
        return ret;
    }

    private Task<QueryData<Foo>> OnQueryAsync(QueryPageOptions options)
    {
        IEnumerable<Foo> items = Items;

        // 设置记录总数
        var total = items.Count();

        // 内存分页
        items = items.Skip((options.PageIndex - 1) * options.PageItems).Take(options.PageItems).ToList();

        return Task.FromResult(new QueryData<Foo>()
        {
            Items = items,
            TotalCount = total
        });
    }

    private bool IsDetails { get; set; } = true;

    private void OnClickDetailRow()
    {
        DetailText = Localizer[$"{nameof(DetailText)}{IsDetails}"];
        IsDetails = !IsDetails;
    }
}
